﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Possy
{
    public partial class PartyPossyAppointmentEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyPossyAppointmentAdd";
            }
        }

        #endregion

        #region 页面属性
      
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
              btnClose.OnClientClick = ActiveWindow.GetHideReference();
            ControlBusiness.ControlBindDataDictionary(this.ddlVisitReason, SysDataDictionary.PossyVisitReason);
            ControlBusiness.ControlBind(this.ddlPossy, PartyPossyInfo.GetGridData(0, int.MaxValue, "", ""), "Name", "ID");
        }

        private void LoadData()
        {
         
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                var appointment = DB.PartyPossyAppointments.Find(id);
                if (appointment == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                ppApplyBranch.Text= ControlBusiness.GetBranchPickerText(appointment.ApplyBranch);
                ppApplyBranch.Value = appointment.ApplyBranch;
                txtDesc.Text = appointment.Describe;
                txtLeader.Text = appointment.Leader;
                txtLeaderPosition.Text = appointment.LeaderPosition;
                txtMobileNo.Text = appointment.MobileNo;
                txtPeopleCount.Text = appointment.PeopleCount.ToString();
                var possy = DB.PartyPossyInfos.Find(appointment.PossyID);
                if (possy != null)
                {
                    txtPossyBranch.Text = ControlBusiness.GetBranchPickerText(possy.BranchID);
                }
                txtUserName.Text = appointment.UserName;
                ddlAppointmentTimeSpen.SelectedValue = appointment.AppointmentTimeSpen;
                ddlPossy.SelectedValue = appointment.PossyID;
                ddlVisitReason.SelectedValue = appointment.VisitReason;
                dtAppointmentTime.Text = Convert.ToDateTime(appointment.AppointmentTime).ToString("yyyy-MM-dd");
                if (appointment.Status == 1 || appointment.Status == 2)
                {
                    AduitInfo.Enabled = true;
                    laAduitBy.Text = ControlBusiness.GetMembersPickerText(appointment.ModifyBy);
                    laAduitTime.Text = Convert.ToDateTime(appointment.ModifyTime).ToString("yyyy-MM-dd HH:mm");
                    laStatus.Text = appointment.Status == 1 ? "同意" : "不同意";
                    

                }
                else
                {
                    AduitInfo.Enabled = false;
                }
                btnAgree.Hidden = appointment.Status > 0;
                btnDisAp.Hidden = appointment.Status > 0;
            }
        }

        public void Update(int Status)
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                var appointment = DB.PartyPossyAppointments.Find(id);
                if (appointment == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                appointment.Status = Status;
                appointment.ModifyBy = CurrentUser.LoginID;
                appointment.ModifyTime = DateTime.Now;

                if (DB.SaveChanges() > 0)
                {
                    PartyTaskHelper.TaskDone(appointment.ID, CurrentUser.LoginID);
                }
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }

        #endregion

        #region Events

        private void SaveData()
        {
            
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #endregion

        protected void btnAgree_Click(object sender, EventArgs e)
        {
            Update(1);
        }

        protected void btnDisAp_Click(object sender, EventArgs e)
        {
            Update(2);
        }
    }
}